﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmLocationGoods : Form
    {
        public FrmLocationGoods()
        {
            InitializeComponent();
        }

        private static List<LocationGoodsInfo> _lstLocationGoods = null;
        public static List<LocationGoodsInfo> lstLocationGoods
        {
            get
            {
                if (_lstLocationGoods == null) _lstLocationGoods = new List<LocationGoodsInfo>();
                return _lstLocationGoods;
            }
        }
        private static LocationGoodsInfo _LocationGoods = new LocationGoodsInfo();

        private void FrmLocationGoods_Load(object sender, EventArgs e)
        {

        }
        //提交
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (lstLocationGoods != null && lstLocationGoods.Count > 0)
                {

                    var re = Duty.LocationGoodsUIRule.NewInstance.SetLocationGoods(LoginBean.Instance.USERCODE, lstLocationGoods);


                    if ( re.DATA)
                    {
                        MessageBox.Show("提交成功！");
                        _lstLocationGoods = null;
                        this.LocationGoodsSource.DataSource = null;
                        this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
                        this.dbGridLocationGoods.Refresh();
                        this.txtGoodsCode.Text = string.Empty;
                        this.txtLocationCode.Text = string.Empty;
                        this.txtGoodsCode.Focus();
                        
                    }
                    else
                    {
                        MessageBox.Show(re.MESSAGE);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        //上架商品扫描验证
        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                string GoodsCode = this.txtGoodsCode.Text.Trim();
                if (string.IsNullOrEmpty(GoodsCode))
                {
                    MessageBox.Show("扫描上架商品条码不能为空！");
                    this.txtGoodsCode.Focus();
                    return;
                }
                if (GoodsCode.Length < 9)
                {
                    MessageBox.Show("上架商品条码不能小于9位数！");
                    this.txtGoodsCode.SelectAll();
                    return;
                }
                if (GoodsCode.Substring(0, 1) != "0" && GoodsCode.Substring(0, 1) != "1" && GoodsCode.Substring(0, 3) != "WOC")
                {
                    MessageBox.Show("请扫描‘0’或‘1’或‘WOC’开头的商品条码！");
                    this.txtGoodsCode.SelectAll();
                    return;
                }

                 
                Cursor.Current = Cursors.WaitCursor;
                //_LocationGoods.GOODSCODE = GoodsCode.Substring(0,10);
                this.txtLocationCode.Focus();
                this.txtLocationCode.SelectAll();
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); 
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        //下架商品编码扫描验证
        //private void txtGoodsCode3_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode != Keys.Enter) return;
        //    try
        //    {
        //        string GoodsCode3 = this.txtGoodsCode3.Text.Trim();
        //        if (string.IsNullOrEmpty(GoodsCode3))
        //        {
        //            MessageBox.Show("扫描下架商品条码不能为空！");
        //            this.txtGoodsCode.Focus();
        //            return;
        //        }
        //        if (GoodsCode3.Length < 9)
        //        {
        //            MessageBox.Show("下架商品条码不能小于9位数！");
        //            this.txtGoodsCode3.SelectAll();
        //            return;
        //        } 
        //        Cursor.Current = Cursors.WaitCursor;
        //        //_LocationGoods.GOODSCODE3 = GoodsCode3.Substring(0,10);
        //        this.txtLocationCode.Focus();
        //        this.txtLocationCode.SelectAll();
        //    }

        //    catch (Exception ex)
        //    {
        //        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);

        //    }
        //    finally
        //    {
        //        Cursor.Current = Cursors.Default;
        //    }
        //}
        //货位扫描验证
        private void txtLocationCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                string GoodsCode = this.txtGoodsCode.Text.Trim();
                
                if (string.IsNullOrEmpty(GoodsCode))
                {
                    MessageBox.Show("请先扫描上架商品条码！");
                    this.txtGoodsCode.Focus();
                    return;
                }
                if (GoodsCode.Substring(0, 1) != "0" && GoodsCode.Substring(0, 1) != "1" && GoodsCode.Substring(0, 3) != "WOC")
                {
                    MessageBox.Show("请扫描‘0’或‘1’或‘WOC’开头的商品条码！");
                    this.txtGoodsCode.SelectAll();
                    return;
                }
                if (GoodsCode.Length < 9)
                {
                    MessageBox.Show("上架商品条码不能小于9位数！");
                    this.txtGoodsCode.Focus();
                    this.txtGoodsCode.SelectAll();
                    return;
                }
                //string GoodsCode3 = this.txtGoodsCode3.Text.Trim();
                //if (string.IsNullOrEmpty(GoodsCode3))
                //{
                //    MessageBox.Show("请先扫描下架商品条码！");
                //    txtGoodsCode3.Focus();
                //    return;
                //}
                //if (GoodsCode3.Length < 9)
                //{
                //    MessageBox.Show("下架商品条码不能小于9位数！");
                //    this.txtGoodsCode3.Focus();
                //    this.txtGoodsCode3.SelectAll();
                //    return;
                //}
                string LocationCode = this.txtLocationCode.Text.Trim();
                if (string.IsNullOrEmpty(LocationCode))
                {
                    MessageBox.Show("请扫描货位编码！");
                    this.txtLocationCode.Focus();
                    return;
                }
                    
                Cursor.Current = Cursors.WaitCursor;
                if (GoodsCode.Length == 17 || GoodsCode.Length == 18 || GoodsCode.Length == 20)
                {
                    _LocationGoods.GOODSCODE = GoodsCode.Substring(0, GoodsCode.Length-8);
                }
                else
                {
                    _LocationGoods.GOODSCODE = GoodsCode; 
                }
                //if (GoodsCode3.Length == 17 || GoodsCode3.Length == 18 || GoodsCode3.Length == 20)
                //{
                //    _LocationGoods.GOODSCODE3 = GoodsCode3.Substring(0, GoodsCode.Length - 8);
                //}
                //else
                //{
                //    _LocationGoods.GOODSCODE3 = GoodsCode3; 
                //}
                 

                _LocationGoods.LOCATIONCODE = LocationCode;
                if (lstLocationGoods.FindIndex(x => x.GOODSCODE == _LocationGoods.GOODSCODE) < 0)
                {
                    lstLocationGoods.Add(_LocationGoods);
                }
                else
                {
                    MessageBox.Show("该上架商品已添加！");
                    txtGoodsCode.Focus();
                    txtGoodsCode.SelectAll();
                    return;
                }
                this.LocationGoodsSource.DataSource = null;
                this.LocationGoodsSource.DataSource = lstLocationGoods;
                this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
                this.dbGridLocationGoods.Refresh();
                _LocationGoods = new LocationGoodsInfo();
                this.txtGoodsCode.Text = string.Empty;
                //this.txtGoodsCode3.Text = string.Empty;
                this.txtLocationCode.Text = string.Empty;
                this.txtGoodsCode.Focus();

            } 
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
       

        private void FrmLocationGoods_Closed(object sender, EventArgs e)
        {
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
            //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
            //Object o = Activator.CreateInstance(type);
            //System.Reflection.MethodInfo mi = type.GetMethod("Show");

            //mi.Invoke(o, null);
            lstLocationGoods.Clear();
            
            this.Close();
            this.Dispose();
        }

        private void FrmLocationGoods_Closing(object sender, CancelEventArgs e)
        {
            if (MessageBoxEx.Question("退出将清空数据，是否退出？") == DialogResult.No)
            {
                e.Cancel = true;
            }
            
        }
    
        //双击删除数据
        private void dbGridLocationGoods_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = this.dbGridLocationGoods.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string goodsCode = dbGridLocationGoods[curIndex, 0].ToString();
                LocationGoodsInfo curItem = lstLocationGoods.Find(x => x.GOODSCODE == goodsCode); 
                string ltCode = curItem.LOCATIONCODE;  

                string Message = string.Format("货位:{0}, 上架商品:{1} \n 是否删除该数据？", ltCode, goodsCode);
                if (MessageBoxEx.Question(Message) == DialogResult.Yes)
                {
                    lstLocationGoods.Remove(curItem);
                    this.LocationGoodsSource.DataSource = null; 
                    this.LocationGoodsSource.DataSource = lstLocationGoods;
                    this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
                    this.dbGridLocationGoods.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            { 

            }

        }

       

       
    }
}